<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\common;

abstract class BaseModelController extends AdminController
{
    protected $model_name;

    public function index()
    {
        $pagination = $this->_get_pagination_data();

        $pagination['rows_count'] = model($this->model_name)->admin_query_count($pagination['where_map']);

        $pagination['total_pages'] = ceil($pagination['rows_count'] / $pagination['page_size']);
        $pagination['page_num'] = $pagination['page_num'] > $pagination['total_pages'] ? 1 : $pagination['page_num'];

        $model_list = model($this->model_name)->admin_query_list($pagination['where_map'], $pagination['page_num'], $pagination['page_size'], $pagination['sort_field'], $pagination['sort_order']);

        $this->assign('pagination', $pagination);
        $this->assign($this->request->action() . '_list', $model_list);
        $this->display('admin/' . $this->request->action() . '/' . $this->request->action() . '_list');
    }

    public function create()
    {
        $model_info = model($this->model_name)->admin_create_object();

        if (request()->isPost()) {
            $model_info->acceptData($this->post_array());

            try {
                $model_info->admin_update_object();
            } catch (Exeption $e) {
                $this->error($e->getMessage());
            }

            $this->success('新增成功。', $this->url('index'));
        }

        $this->assign($this->request->action() . '_info', $model_info);
        $this->display('admin/' . $this->request->action() . '/' . $this->request->action() . '_form');
    }

    public function modify($id)
    {
        $model_info = model($this->model_name)->admin_retrieve_object($id);

        if (request()->isPost()) {
            $model_info->acceptData($this->post_array());

            try {
                $model_info->admin_update_object();
            } catch (Exeption $e) {
                $this->error($e->getMessage());
            }

            $this->success('修改成功。', $this->url('index'));
        }

        $this->assign($this->request->action() . '_info', $model_info);
        $this->display('admin/' . $this->request->action() . '/' . $this->request->action() . '_form');
    }

    public function bulk_delete($ids = array())
    {
        $model_list = model($this->model_name)->where('id', 'in', $ids)->select();

        foreach ($model_list as $model_info) {
            try {
                $model_info->admin_delete_object();
            } catch (Exeption $e) {
                $this->error($e->getMessage());
            }
        }

        $this->success('删除成功。', $this->url('index'));
    }
}

